#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <QItemSelectionModel>
#include <QToolButton>
#include <QFontComboBox>
#include <QActionGroup>
#include <QPageSetupDialog>

#include "CAnswer.h"
#include "CListQuestion.h"
#include "CPrintTest.h"
#include "ctestmodel.h"
#include "canswerform.h"
#include "def.h"
#include "textedit/ctextedit.h"
#include "CPaginationForm.h"
#include "CPagination.h"
#include "caboutdialog.h"
#include "cappsettingsdlg.h"
#include "CImageEditor.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    CTestModel *testModel;
    QSortFilterProxyModel *proxyModel;
    QItemSelectionModel *selectionModel;

    QVBoxLayout *rightVbl;
    QVBoxLayout *testsVbl;
    QWidget *testsWidget;

    QVector<CAnswerForm*> answerList; //вектор виджетов для вывода ответов
    CAnswerForm *curAnswerForm;

    QActionGroup *grpAlign;
    QActionGroup *grpFormating;
    QActionGroup *grpQuest;
    QActionGroup *grpPrint;

//    QAction *actionAlignLeft;
//    QAction *actionAlignCenter;
//    QAction *actionAlignRight;
//    QAction *actionAlignJustify;
//    QAction *paginationAction;


    //tool button
    QToolButton *loadBtn;
    QToolButton *newBtn;
    QToolButton *saveBtn;
    QToolButton *printBtn;
    QToolButton *addQuest;
    QToolButton *delQuest;
    QToolButton *saveQuestBtn;
    QToolButton *addAnswerBtn;
    QToolButton *delAnswerBtn;
    QToolButton *addPictBtn;
    QFontComboBox *fontComboBox;
    QComboBox *fontSizeComboBox;
    QToolButton *boldBtn;
    QToolButton *italicBtn;
    QToolButton *underBtn;
    QToolButton *centerAlignBtn;
    QToolButton *leftAlignBtn;
    QToolButton *rightAlignBtn;
    QToolButton *jusifyAlignBtn;
    QToolButton *colorBtn;

    //счетчик добавленных ответов в редактируемый вопрос
    int countAddAnswer;
    QString t2pFile;

    //объекта класса CTextEdit имеющий фокус в данный момент
    CTextEdit *curTextEdit;
    //объекта класса CTextEdit потерявший фокус
    CTextEdit *prevCurTextEdit;
    //форма с настройками
    CPaginationForm *paginationForm;
    CAboutDialog *aboutDlg;
    QPageSetupDialog *pageSetupDlg;
    CPageSettings *pageSettings;
    CAppSettingsDlg *appSettingsDlg;
    //класс с настройками, возвращается settingForm
    //CPrintSettings *printSettings;
    //контекстное меню списка вопросов (questListView)
    //QPopupMenu viewListPopup;

//    CPrintTest *printTest;
private: //methods
    void createAction();
    void makeInterface();
    void createStandardToolBar();
    void createFormattingToolBar();
    void restoreAppSettings();
    void saveSettings();
    void initModel();

    int mbContinue(QString &text, QString title = "Test2Print");
    void setTitle(QString &text);
    void deleteAnswerForms();

private slots:
    void on_actionAddImage_triggered();
    void on_actionPageSetup_triggered();
    void on_actionPreview_triggered();
    void on_printKeysAction_triggered();
    void on_printTestAction_triggered();
    void on_saveAction_triggered();
    void on_newAction_triggered();
    void on_actionSaveQuestion_triggered();
    void on_actionDelAnswer_triggered();
    void on_actionAddAnswer_triggered();
    void on_actionDelQuestion_triggered();
    void on_actionAddQuestion_triggered();
    void on_openAction_triggered();
    void on_actionBold_triggered();
    void on_paginationAction_triggered();
    void setWidgets(QWidget* oldWidget, QWidget* newWidget);
    void on_commentTextEdit_textChanged();
    void on_saveAsAction_activated();
    void on_questTextEdit_textChanged();
    void init();
    bool openFile(QString t2pFile = QString());
    void saveFile();
    void saveAsFile();
    void addPict();
    bool imageExtension( QString fileName);
    void showSelectedItem( const QItemSelection & selected, const QItemSelection & deselected ); //Выбран вопрос из списка
    void saveItem();//сохранение вопроса
    void rightAnswerChecked(); //Поставлена/снята отметка на ответе
    void answerChanged(); //изменен ответ
    void commentChanged(); //изменен коментарий
    void answerClicked(CAnswerForm *answerForm);
    void currentCharFormatChanged(const QTextCharFormat &format);
    void makeAlignmentChanged(Qt::Alignment a);
    void makeTextAlign(QAction *a);
    void fontChanged(const QFont &f);
    void setTextFamily(const QString &f);
    void setTextSize(const QString &f);
    void enabledInterface();
    void on_printCommentAction_triggered();
    void on_actionItalic_triggered();
    void on_actionUnderline_triggered();
    void on_aboutMenu_triggered();
    void on_actionAppSettings_triggered();
    void on_actionFontColor_triggered();
    void colorChanged(const QColor &c);
    void mergeFormatOnWordOrSelection(const QTextCharFormat &format);
    void dblClickOnTextEdit(CTextEdit *textEdit);

signals:
    void answerAdded(const int &nQuestion, const int &count);
};


#endif // MAINWINDOW_H
